﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace _010_RemotingClientTCPActivatedAsync
{
    class Program
    {
        delegate void RemoteAsyncDelegate(int rq_id);

        RemoteAsyncDelegate del = null;
        int rq_index;
        [OneWay]
        public void Done(IAsyncResult res)
        {
            del.EndInvoke(res);
            _10_RemotingSharedAssembly.IServerInterface srv = (_10_RemotingSharedAssembly.IServerInterface)res.AsyncState;
            Console.WriteLine("Done");

            _10_RemotingSharedAssembly.ServerResponse resp = srv.GetResponse(rq_index);
            foreach (int a in resp.Array) Console.Write("{0} ", a);
            Console.WriteLine();
        }

        public Program()
        {
            try
            {
                TcpChannel channel = new TcpChannel();
                ChannelServices.RegisterChannel(channel, true);

                ActivatedClientTypeEntry entry = new ActivatedClientTypeEntry(typeof(_010_RemotingServerTCPActivated.RemotingServer), "tcp://localhost:9999");
                RemotingConfiguration.RegisterActivatedClientType(entry);

                _10_RemotingSharedAssembly.IServerInterface srv = new _010_RemotingServerTCPActivated.RemotingServer();


                Console.WriteLine("Enter array size:");
                int size = Int32.Parse(Console.ReadLine());
                if (size > 0)
                {
                    _10_RemotingSharedAssembly.ServerRequest rq = new _10_RemotingSharedAssembly.ServerRequest();
                    rq.Array = new int[size];
                    for (int i = 0; i < size; i++)
                    {
                        Console.Write("Enter element {0}:", i);
                        rq.Array[i] = Int32.Parse(Console.ReadLine());
                    }
                    Console.Write("Sending request to server ...");
                    rq_index = srv.AddRequest(rq);
                    Console.WriteLine("{0}", rq_index);

                    rq = srv.GetRequest(rq_index);
                    foreach (int a in rq.Array) Console.Write("{0} ", a);
                    Console.WriteLine();

                    Console.WriteLine("Processing request ...");

                    AsyncCallback callback = new AsyncCallback(this.Done);
                    del = new RemoteAsyncDelegate(srv.ProcessRequest);

                    IAsyncResult result = del.BeginInvoke(rq_index, callback, srv);
                    Console.WriteLine("Waiting ...");
                    //while (result.AsyncWaitHandle.WaitOne(new) { Console.Write(".")}
                    result.AsyncWaitHandle.WaitOne();
//                    srv.ProcessRequest(rq_index);
                    Console.WriteLine("Done at all");

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static void Main(string[] args)
        {
            new Program();
            Console.ReadLine();
        }
    }
}
